<?php
namespace app\open\controller;
use think\Controller;
use think\Db;

class Show extends Controller
{
    public function index(){
        echo 'channel-show';
    }

    public function login(){
        if(request()->isAjax()){
            $where['account'] = input('account');
            $where['password'] = md5(input('password'));
            if( ! $find = Db::name('CpAccount')->where($where)->find() ){
                $this->layJson(1, '账号或者密码错误');
            }

            if($find['status']!==1){
                $this->layJson(1, '该账号被禁用，请联系管理员');
            }

            session('admin.account', $find['account']);
            session('admin.password', $find['password']);
            session('admin.start_time', time());
            if($find['login_time']){
                session('admin.login_time', date('Y-m-d H:i:s', $find['login_time']));
            }else{
                session('admin.login_time', '公元前');
            }
            session('admin.id', $find['id']);
            session('admin.name', $find['username']);
            if($find['app_id']){
                $gameArr = model('ViewGameManage')->where('app_id', 'in', $find['app_id'])->column('game_id');
                $gameId = $gameArr ? implode(',', $gameArr) : 0;
            }else{
                $gameId = 0;
            }
            session('admin.game_id', $gameId);

            $update['login_time'] = time();
            $update['login_ip'] = request()->ip();
            Db::name('CpAccount')->where('id', $find['id'])->update($update);

            $this->layJson(0, '登录中....');
        }else{
            return view();
        }
    }

    protected function layJson($code=0, $msg='', $data=[], $count=0){
        $json['code'] = $code;
        $json['msg'] = $msg;
        $json['data'] = $data;
        $json['count'] = $count;
        echo json_encode($json);exit;
    }
}